// void, Obj This

Query qEnemy, qOutpost;
ObjList ol_enemy, ol_outpost, ol_village, ol_hero, ol_tw;
Building outpost, village, tw;
Hero h;
Unit this;
int r, i, timetoattack;
int barbadd, chance;
int chooseplayer, randdirect, mingold;
int ucount, rlimit, pcount, ci;
IntArray goldplayer, mingoldplayer;
	
Sleep(940+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsUnit();

//timetoattack = 1;
timetoattack = 5 * 60000;
barbadd = 0;

if(EnvReadInt("AttackTo") != 20)
	EnvWriteInt("AttackTo", 20);

while(!.IsValid) Sleep(10000); 

if(.IsValid){
	chance = 0;
    if(EnvReadInt("StopBarbarianItem") != 0){ 
		chance = 40;
	}
	//r = rand(4);
	if(rand(1000) < (750-(chance*18))){
		if(!.HasItem("Pouch Gold")){
			Item item;
							
			.AddItem("Pouch Gold");
			item = .GetItem(1);
				
			if (item.IsValid){
				if(item.name == "Pouch Gold"){
					if(.class == "RebelJavelinThrower")
						item.SetUseCount(rand(900)+100);
					else 
						item.SetUseCount(rand(1300)+200);
				}
			}
		}
	}
	if(rand(1000) < 100-chance){
		str nameItem;
		int randItem;
			
		randItem = rand(2);
		if(randItem == 0)
			nameItem = "Veteran Offence";
		else if(randItem == 1)
			nameItem = "Veteran Defence";
				
		.AddItem(nameItem);
		
	} else if (rand(1000) < (50-chance)){
		str nameItem;
		int randItem;
			
		randItem = rand(24);

		if(randItem == 0)
			nameItem = "Battle drums of rage";
		else if(randItem == 1)
			nameItem = "Sign of the hammer";
		else if(randItem == 2)
			nameItem = "Ghost's skull";
		else if(randItem == 3)
			nameItem = "Gold of resource";
		else if(randItem == 4)
			nameItem = "Food of resource";
		else if(randItem == 5)
			nameItem = "Assassin sword";
		else if(randItem == 6)
			nameItem = "Assassin bow";
		else if(randItem == 7)
			nameItem = "Herb amulet of luck";
		else if(randItem == 8)
			nameItem = "Belt of snakes";
		else if(randItem == 9)
			nameItem = "--Feather amulet--";
		else if(randItem == 10)
			nameItem = "King's Belt";
		else if(randItem == 11)
			nameItem = "Elephant Tusk";
		else if(randItem == 12)
			nameItem = "Boar teeth";
		else if(randItem == 13)
			nameItem = "Horn of victory";
		else if(randItem == 14)
			nameItem = "Ash of druid heart";
		else if(randItem == 15)
			nameItem = "Finger of death";
		else if(randItem == 16)
			nameItem = "Fur gloves of health";
		else if(randItem == 17)
			nameItem = "Concentration stone";
		else if(randItem == 18)
			nameItem = "Gem of Power";
		else if(randItem == 19)
			nameItem = "Amulet of Triumph";
		else if(randItem == 20)
			nameItem = "Golden Ankh";
		else if(randItem == 21)
			nameItem = "Gem of Wisdom";
		else if(randItem == 22)
			nameItem = "Ring of Power";
		else if(randItem == 23)
			nameItem = "Scroll of Death";

		.AddItem(nameItem);
		.AddBonus(randItem,0,0,0,0);
	}
}
        
Sleep(13500);

while(.IsValid){
	if(.player == 15){
	//pr("rebel liberati");

	if(.InHolder){
		Sleep(2115);
		outpost = .GetHolderSett.GetCentralBuilding(); 
		qEnemy = Intersect(ObjsInCircle(outpost.pos, 1000, "Military"), EnemyObjs(.player, "Military")); 
		qEnemy.GetObjList().ClearDead;
		if(!qEnemy.IsEmpty){
			ol_enemy = qEnemy.GetObjList();
			r = rand(ol_enemy.count);
			if(ol_enemy[r].IsValid)
				.SetCommand("advance", ol_enemy[r].posRH+Point(-10, 10));
		} 
	} else {
		for (i=0; i<20; i+=1){
			Sleep(5000);
			if(.InHolder) break;	
		}
		
		qEnemy = Intersect(ObjsInCircle(.posRH, 1000, "Military"), EnemyObjs(.player, "Military"));
		qEnemy.GetObjList().ClearDead;
		ol_enemy = qEnemy.GetObjList();
		if(ol_enemy.count >= 6){
			r = rand(ol_enemy.count);
			if(ol_enemy[r].IsValid)
				.SetCommand("advance", ol_enemy[r].posRH+Point(-10, 10));
		} else {
			if(.command == "stand_position" || .command == "idle")
				qOutpost = ObjsInCircle(.posRH, 1400, "Outpost");
			else
				qOutpost = ObjsInCircle(.posRH, 910, "Outpost");

			if(!qOutpost.IsEmpty){
				ol_outpost = qOutpost.GetObjList();
				outpost = ol_outpost[0].AsBuilding();
						
				if(outpost.IsValid){
					if(!.IsEnemy(outpost)){
						.SetCommand("enter", outpost.settlement.GetCentralBuilding);
					} else if(.IsEnemy(outpost)){
						if(outpost.settlement.UnitsCount <= 6){
							.SetCommand("capture", outpost.settlement.GetCentralBuilding);  
						} else if(outpost.settlement.UnitsCount > 6){
						.SetCommand("stand_position");          
						}
					}
				}
			} else {
				if(.command == "idle" || .command == "stand_position"){
					r = rand(4);
					if(r == 0) 
						.SetCommand("advance", .posRH+Point(150, 150));		
					else if(r == 1)
						.SetCommand("advance", .posRH+Point(-150, -150)); 
					else if(r == 2)
						.SetCommand("advance", .posRH+Point(150, -150));
					else 
						.SetCommand("advance", .posRH+Point(-150, 150));
				}
			}
		}
	}
	Sleep(1000);

	//timeadd += 1;
	if(.GetHolderSett.IsValid){
		if(.GetHolderSett.GetCentralBuilding.IsHeirOf("BaseTownhall"))
			continue;
	}
	if(.item_count == 0)
	if(.command != "capture")
	if(GetTime() > timetoattack){
		//barbadd = EnvReadInt(.player, "CollectionBarb");
		//EnvWriteInt(.player, "CollectionBarb", barbadd+1);
		//while(1){
			//Sleep(2000);
			//if(EnvReadInt(.player, "CollectionBarb") > 24)
				//break;
		//}
		//pr(timetoattack/1000);
		//timeadd = 0;
		pcount = EnvReadInt("PlayerCount");
		timetoattack += timetoattack;
		//randplayer = rand(pcount)+1;
		//randplayer = 2;
		//pr("pcount: "+pcount);
		for (i=0; i<pcount; i+=1){
			goldplayer[i] = EnvReadInt(i+1, "PayBarbarianCount");
			Sleep(100);
		}
		//pr(goldplayer[0]);
		if(pcount == 1){
			chooseplayer = 1;
		} else if(pcount == 2){
			mingold = MIN(goldplayer[0], goldplayer[1]);
		} else if(pcount == 2){
			mingold = MIN(goldplayer[0], goldplayer[1]);
		} else if(pcount == 3){
			mingold = MIN(goldplayer[0], MIN(goldplayer[1], goldplayer[2]));
		} else if(pcount == 4){
			mingold = MIN(MIN(goldplayer[0], goldplayer[1]), MIN(goldplayer[2], goldplayer[3]));
		} else if(pcount == 5){
			mingold = MIN(MIN(MIN(goldplayer[0], goldplayer[1]), MIN(goldplayer[2], goldplayer[3])), goldplayer[4]);
		} else if(pcount == 6){
			mingold = MIN(MIN(MIN(goldplayer[0], goldplayer[1]), MIN(goldplayer[2], goldplayer[3])), MIN(goldplayer[4], goldplayer[5]));
		} else if(pcount == 7){
			mingold = MIN(MIN(MIN(MIN(goldplayer[0], goldplayer[1]), MIN(goldplayer[2], goldplayer[3])), MIN(goldplayer[4], goldplayer[5])), goldplayer[6]);
		} else if(pcount == 8){
			mingold = MIN(MIN(MIN(MIN(goldplayer[0], goldplayer[1]), MIN(goldplayer[2], goldplayer[3])), MIN(goldplayer[4], goldplayer[5])), MIN(goldplayer[6], goldplayer[7]));
		} 
		ci = 0;
		//pr("gold p1: ");
		//pr(goldplayer[0]);
		//pr("gold p2: ");
		///pr(goldplayer[1]);
		//pr("gold p3: ");
		//pr(goldplayer[2]);
		//pr(pcount);
		for (i=0; i<pcount; i+=1){
			//pr(goldplayer[i]);
			if(mingold == goldplayer[i]){
				mingoldplayer[ci] = i+1;
				ci+=1;
			}
			Sleep(100);
		}
		//pr("mingold");
		//pr(mingold);
		//pr("ci");
		//pr(ci);
		chooseplayer = mingoldplayer[rand(ci)];
		//pr("player: "+chooseplayer);
		if(rand(100) < 80){
			if(EnvReadInt("AttackTo") == 20){
				EnvWriteInt("AttackTo", rand(4));
				
				randdirect = EnvReadInt("AttackTo");
			}
		} else randdirect = rand(4);
		Sleep(500);
		//pr("direct: "+randdirect);
		if(randdirect == 0){
			
			ol_outpost = ClassPlayerObjs("Outpost", chooseplayer).GetObjList();
			for (i=0; i<ol_outpost.count; i+=1){
				
				rlimit = 10+rand(25);
				outpost = ol_outpost[i].AsBuilding();
				
				ucount = outpost.settlement.UnitsCount;
				if(ucount < rlimit){
					if(outpost.IsValid){
						if(.IsEnemy(outpost)){
							.SetCommand("advance", outpost.settlement.GetCentralBuilding.pos);  
							.AddCommand(false, "capture", outpost.settlement.GetCentralBuilding);
							//pr("Capture to outpost");
							break;
						}
					} else { randdirect = rand(4); break;}
				} else { randdirect = rand(4); break;}
				Sleep(100);
			}
		} else if(randdirect == 1){
			
			ol_village = ClassPlayerObjs("BaseVillage", chooseplayer).GetObjList();
			
			village = ol_village[rand(ol_village.count)].AsBuilding();

			if(village.IsValid){
				if(.IsEnemy(village)){
					.SetCommand("advance", village.settlement.GetCentralBuilding.pos); 
					.AddCommand(false, "capture", village.settlement.GetCentralBuilding);
					//pr("Capture to village");
				}
			} else { randdirect = rand(4); }
		} else if(randdirect == 2){
			
			ol_tw = ClassPlayerObjs("BaseTownhall", chooseplayer).GetObjList();
			
			tw = ol_tw[rand(ol_tw.count)].AsBuilding();

			if(tw.IsValid){
				if(.IsEnemy(tw)){
					.SetCommand("advance", tw.settlement.GetCentralBuilding.pos);
					.AddCommand(false, "capture", tw.settlement.GetCentralBuilding);
					//pr("Capture to townhall");
				}
			} else { randdirect = rand(4); }
		} else if(randdirect == 3){
			
			ol_hero = ClassPlayerObjs("Hero", chooseplayer).GetObjList();
			for (i=0; i<ol_hero.count; i+=1){
				//h = ol_hero[rand(ol_hero.count)].AsHero();
				h = ol_hero[rand(ol_hero.count)].AsHero();
				if(h.IsValid){
					if(.IsEnemy(h)){
						.SetCommand("advance", h.posRH);
						//pr("Advance to hero");
						break;
					}
				} else { randdirect = rand(4); }
				Sleep(100);
			}
		}
	}
	} else {
		while(.IsValid) Sleep(60*1000);
	}
}
Sleep(1000);
